﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using PloobsEngine.SceneControl;
using PloobsEngine.Physics;


namespace PloobsEngine.IA
{
    public class VisibilitySensor : ISensor<IList<VisibilitySensorInformation>>
    {
        private IWorld world;
        private IObject obj;
        private float altura = 10;
        private float TamanhoDoRaio = 50;
        

        public float Distance
        {
            get { return TamanhoDoRaio; }
            set { TamanhoDoRaio = value; }
        }

        public float Altura
        {
            get { return altura; }
            set { altura = value; }
        }
        private float distanciaDoCentroDeMassa = 10;

        public float DistanciaDoCentroDeMassa
        {
            get { return distanciaDoCentroDeMassa; }
            set { distanciaDoCentroDeMassa = value; }
        }

        public VisibilitySensor(IWorld world,IObject obj)
        {
            this.world = world;
            this.obj = obj;
        }
        #region ISensor Members

        public IList<VisibilitySensorInformation> DetectEnviroment()
        {
            Vector3 dir = obj.PhysicObject.FaceVector;
            dir.Normalize();
            Ray raio = new Ray(obj.Position + new Vector3(0,altura,0) + dir * distanciaDoCentroDeMassa ,dir);

            ///////So pra debug Remover isso depois
            
            //DebugVectorDraw.addVector(raio.Position, dir * (TamanhoDoRaio - distanciaDoCentroDeMassa), Color.Red);
            //////

            RayTestInfo ri = world.SegmentIntersect(raio,SegmentInterceptMethod.ALL,TamanhoDoRaio);
            if (ri == null)
                return new List<VisibilitySensorInformation>();
            else
            {
                List<VisibilitySensorInformation> l = new List<VisibilitySensorInformation>();
                if (ri.Object.Agente == null)
                {
                    VisibilitySensorInformation si = new VisibilitySensorInformation();
                    si.Agente = new NotAgent(ri.Object);
                    si.RayTestInfo = ri;
                    l.Add(si);
                }
                else
                {
                    VisibilitySensorInformation si = new VisibilitySensorInformation();
                    si.Agente = ri.Object.Agente;
                    si.RayTestInfo = ri;
                    l.Add(si);
                }
                return l;
            }
        }

        #endregion

        #region ISensor<VisibilitySensorInformation> Members

        
        
        #endregion
    }
}
